package com.liu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.liu.bean.AccountDetail;
import com.liu.bean.Enterprise;
import com.liu.bean.XuFei;
import com.liu.common.crud.BaseServiceImpl;
import com.liu.common.exception.BusinessException;
import com.liu.exccel.EnterpriseData;
import com.liu.mapper.AccountDetailMapper;
import com.liu.mapper.EnterpriseCateforyMapper;
import com.liu.mapper.EnterpriseMapper;
import com.liu.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

@Service
public class EnterpriseServiceImpl extends BaseServiceImpl<Enterprise, EnterpriseMapper> implements EnterpriseService {

    private String currentMaxCode ; //当前最大编码

    @Autowired
    AccountDetailMapper accountDetailMapper;

    @Autowired
    EnterpriseCateforyMapper enterpriseCateforyMapper;

    @Autowired
    HttpServletRequest request;

    @Transactional
    @Override
    public int save(Enterprise entity) {

        //检查企业名是否已经存在
        int count = baseMapper.existName(entity.getName());
        if(count>0){
            //存在
            throw new BusinessException("企业名称已存在！");
        }
        //不存在,生成编码
        entity.setCode(generateNewCode());
        //保存企业
        int rs = super.save(entity);
        if(rs==0)
            throw new BusinessException("保存企业失败");
        rs = enterpriseCateforyMapper.addEnterpriseNum(entity.getCategoryCode());
        return rs;
    }

    //生成新的编码
    String generateNewCode(){
        if(null!=currentMaxCode){
            return getNextCode(currentMaxCode);
        }

        //内存美哟最大编码，去数据库查询
        currentMaxCode = baseMapper.querymaxCode();
        if(null==currentMaxCode){
             currentMaxCode="100001";
             return currentMaxCode;
        }
        //如果存在
        return getNextCode(currentMaxCode);


    }

    String getNextCode(String currentCode){
        int tmp = Integer.parseInt(currentCode);
        tmp++;
        currentMaxCode = String.valueOf(tmp);
        return currentMaxCode;
    }


    /**
     * 权限
     * @param id 企业id
     * @param empNum  可创建员工数
     * @param accountNum 可创建账套数
     * @return
     */
    @Override
    public int auth(int id, int empNum, int accountNum) {
        return baseMapper.auth(id,empNum,accountNum);
    }

    @Transactional
    @Override
    public int xvfei(XuFei xuFei) {
        //更新企业金额及时间
        int rs = baseMapper.xvfei(xuFei);
        if (rs == 0) {
            throw new BusinessException("更新企业余额失败");
        }
        if(xuFei.getMonth()>0){
            //如果有增加会员日期  判单是否首次购买会员 如果是更新开通日期
            baseMapper.updateBeginTime(xuFei.getEid());
        }
        //更新成功 添加变动明细
        AccountDetail detail = new AccountDetail();
        detail.setType(2);
        detail.setEid(xuFei.getEid());
        detail.setReason("企业续费" + xuFei.getMonth() + "月");
        detail.setMoney(xuFei.getMoney());
        rs = accountDetailMapper.save(detail);
        return rs;
    }

    @Override
    public Map queryMoney(int id) {
        return baseMapper.queryMoney(id);
    }

    @Override
    public String export(Map map) {
        List<EnterpriseData> list = baseMapper.export(map);
        String root = request.getServletContext().getRealPath("/")+"/excel";
        File file = new File(root);
        if(!file.exists())
            file.mkdirs();
        String newFileName = file.getAbsolutePath()+"/企业列表.xlsx";
        EasyExcel.write(newFileName, EnterpriseData.class)
                .sheet("模板")
                .doWrite(() -> {
                    // 分页查询数据
                    return list;
                });

        return "/excel/企业列表.xlsx";
    }
}
